
//
//  HDSQLiteManager.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/23.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit
import FMDB
//1.创建表  保存在沙盒中
class HDSQLiteManager: NSObject {
    //单例
    static let  sharedManager : HDSQLiteManager = HDSQLiteManager()
    let dbQueue :FMDatabaseQueue
    //指定构造函数
    private override init() {
        //数据路径
        let dbPath =  NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/status.db"
        dbQueue = FMDatabaseQueue(path: dbPath)
        super.init()
        createTable()
       }
    //创建表
    private func createTable(){
        //读取sql数据才创建表
        let tablesPath = NSBundle.mainBundle().pathForResource("tables", ofType: "sql")!
        //读取文件
        let  tables = try! NSString(contentsOfFile:tablesPath, encoding:NSUTF8StringEncoding)
        //执行多条sql语句
        dbQueue.inDatabase { (db) -> Void in
            if db.executeStatements(tables as String){
                print("执行成功")
            }else{
                print("执行失败")
            }
        }
    }
    

}
